import axios from "axios";
import { SERVICE_ID, CTX_PATH } from "@common/constants";
import { getRequest, generateSignStr } from "@common/utils";
import { AclUrls } from "../urls";
import { JSEncrypt } from "jsencrypt";
import Instance from "@common/remoteCallInterceptor";

export default {
  initLogin: () => {
    let req = getRequest(SERVICE_ID.DACHSHUND);
    return axios.post(AclUrls.initLogin, req, {
      baseURL: CTX_PATH.getDachshundPrefix(),
    });
  },

  login: (params) => {
    let {
      modulus,
      sessionId,
      timestamp,
      account,
      secNonce,
      password,
      exponent,
    } = params;
    let rsa = new JSEncrypt();
    rsa.setKey(modulus);

    let encAcct = rsa.encrypt([account, secNonce].join("|"));
    let encPwd = rsa.encrypt([password, exponent].join("|"));

    let headers = {
      "x-session-id": sessionId,
      "x-last-access-time": timestamp,
    };

    let payload = {
      account: encAcct,
      password: encPwd,
      androidId: window.localStorage.getItem("androidId")
    };

    console.log(payload,'payload');
    return axios.post(
      AclUrls.login,
      getRequest(
        SERVICE_ID.DACHSHUND,
        payload,
        generateSignStr(null, [encAcct, encPwd])
      ),
      {
        headers,
        baseURL: CTX_PATH.getDachshundPrefix(),
      }
    );
  },

  logout: () => {
    let req = getRequest(SERVICE_ID.DACHSHUND);
    return Instance.dachshundInstance.post(AclUrls.logout, req, {
      baseURL: CTX_PATH.getDachshundPrefix(),
    });
  },
};
